package com.youshang520i.learningmysqlcanal.controller;

import com.youshang520i.learningmysqlcanal.model.Goods;
import com.youshang520i.learningmysqlcanal.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author youshang
 */
@RestController
@RequestMapping("/canal/goods")
public class GoodsController {

    private static Map returnMap = new HashMap();

    static {
        returnMap.put("code","200");
        returnMap.put("message","ok");
        returnMap.put("data","");
    }

    @Autowired
    private GoodsService goodsService;

    @PutMapping("/add")
    public Map add(Goods goods){
        goods.setGoodsId(UUID.randomUUID().toString());
        if (goodsService.insertSelective(goods) > 0) {
            return returnMap;
        }
        returnMap.put("code","0");
        returnMap.put("message","失败");
        return returnMap;
    }

    @PutMapping("/update")
    public Map update(@RequestBody Goods goods){
        if (goodsService.updateByPrimaryKeySelective(goods) > 0) {
            return returnMap;
        }
        returnMap.put("code","0");
        returnMap.put("message","失败");
        return returnMap;
    }

    @GetMapping("/detail/{goodsId}")
    public Map detail(@PathVariable String goodsId){
        Goods goods = goodsService.selectByPrimaryKey(goodsId);
        returnMap.put("data",goods);
        return returnMap;
    }
}
